import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class OrderCart
 */
public class OrderCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String server = "jdbc:mysql://127.0.0.1/kroketweb";
	private static final String user = "kroketweb";
	private static final String password = "password";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrderCart() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		HashMap<Integer, Integer> products = (HashMap) session
				.getAttribute("mapje");

		PrintWriter out = response.getWriter();
		if (products != null) {
			out.println("er zijn products");
		}
		if (products != null) {
			// voeg alles toe in de database
			try {
				Class.forName("com.mysql.jdbc.Driver");
				Connection con;
				try {
					con = DriverManager.getConnection(server, user, password);
					try {
						Statement st = con.createStatement();

						// Ik ga ervan uit dat de aanroep alle variabelen bevat.
						String name = request.getParameter("nam");
						String address = request.getParameter("adr");
						String town = request.getParameter("tow");
						String phone = request.getParameter("pho");

						out.println(name);
						out.println(address);
						out.println(town);
						out.println(phone);
						
						boolean success = st
								.execute(
										"INSERT INTO orders (order_name, order_address, order_town, order_phone) VALUES ('"
												+ name
												+ "', '"
												+ address
												+ "', '"
												+ town
												+ "', '"
												+ phone + "');",
										Statement.RETURN_GENERATED_KEYS);

							int orderId = st.getResultSet().getInt("order_id");
							for (Entry<Integer, Integer> entry : products
									.entrySet()) {
								int productId = entry.getKey();
								int amount = entry.getValue();
								boolean productSuccess = st
										.execute("INSERT INTO order_product (order_id, product_id, order_product_amount) VALUES ('"
												+ orderId
												+ "', '"
												+ productId
												+ "', '" + amount + "');");
								if (productSuccess) {
									ResultSet rs = st
											.executeQuery("SELECT product_stock FROM product WHERE product_id = '"
													+ productId + "';");
									int oldStock = rs.getInt("product_stock");
									int newStock = oldStock - amount;
									st
											.execute("UPDATE products SET product_stock = '"
													+ newStock + "';");
								}
							}

					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
